package br.perfiman.service.impl;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.ce.wcaquino.Validation.GeneralValidation;
import br.perfiman.dao.AbstractDAOService;
import br.perfiman.dao.AssetDAOService;
import br.perfiman.exceptions.ErrorsConstants;
import br.perfiman.exceptions.InvalidFieldException;
import br.perfiman.exceptions.RequiredFieldException;
import br.perfiman.model.Asset;
import br.perfiman.service.AssetService;

/**
 *
 * @author Wagner Costa
 * @since 27/12/2007
 * @version 1.0
 *
 */
@Stateless
public class AssetServiceBean extends AbstractServiceBean<Asset> implements AssetService{
	@EJB(name="AssetDAOService")
	protected AssetDAOService dao;

	@Override
	public AbstractDAOService<Asset> getDao() {
		return dao;
	}

	@Override
	public void validateBussinessRules(Asset entity, boolean isUpdate) {
		// TODO Auto-generated method stub

	}

	@Override
	public void validateInvalidFields(Asset entity, boolean isUpdate) {
		if(entity.getType() < Asset.TYPE_FULL || entity.getType() > Asset.TYPE_OPTIONS)
			throw new InvalidFieldException(getFormKey("type"), ErrorsConstants.BEETWEN, String.valueOf(Asset.TYPE_FULL), String.valueOf(Asset.TYPE_OPTIONS));

	}

	@Override
	public void validateRequiredFields(Asset entity, boolean isUpdate) {
		RequiredFieldException exception = new RequiredFieldException();
		if(GeneralValidation.isEmpty(entity.getName()))
			exception.add(getFormKey("name"), ErrorsConstants.REQUIRED);
		if(GeneralValidation.isEmpty(entity.getSymbol()))
			exception.add(getFormKey("symbol"), ErrorsConstants.REQUIRED);
		if(GeneralValidation.isEmpty(entity.getType()))
			exception.add(getFormKey("type"), ErrorsConstants.REQUIRED);

		if(exception.hasErrors())
			throw exception;
	}
}
